package com.gkzf.ai.module.crm.dal.redis.product;

import com.alibaba.fastjson.JSON;
import com.gkzf.ai.framework.common.util.json.JsonUtils;
import com.gkzf.ai.module.crm.controller.app.product.vo.ProductDetailsRespVO;
import com.gkzf.ai.module.crm.dal.dataobject.product.CrmProductDO;
import com.gkzf.ai.module.crm.dal.redis.RedisKeyConstants;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

/**
 *  产品信息 redis 公共包
 * @author Huawei
 * &#064;date  2025-04-02 14:24
 */
@Repository
public class CRMProductRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static String formatKey(Long productId) {
        return String.format(RedisKeyConstants.CRM_PRODUCT_ID, productId);
    }
    public void delete(CrmProductDO product) {
        String redisKeyId = formatKey(product.getId());
        stringRedisTemplate.delete(redisKeyId);
    }

    public ProductDetailsRespVO get(Long productId) {
        String redisKey = formatKey(productId);
        return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), ProductDetailsRespVO.class);
    }

    public void setProductInfoById(ProductDetailsRespVO respVO){
        stringRedisTemplate.opsForValue().set(formatKey(respVO.getId()), JSON.toJSONString(respVO));
    }




}
